﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using zhangmenren.Models;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
using zhangmenren.IService;

namespace zhangmenren.Areas.Admin.Controllers
{
    [Area(areaName: "Admin")]
    public class ChapterController : Controller
    {
        private IChapterService chapterService;

        public ChapterController(IChapterService _chapterService) 
        {
            chapterService = _chapterService;
          }


        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Add()
        {
            return View();
        }

        public async Task<string> GetChapter(int workId = 1)
        {
            int total = 0;
            IEnumerable<ChapterEntity> dataList = null;
            dataList = await chapterService.Selelct(workId);
            if (dataList.Any())
            {
                total = dataList.Count();
            }
            return JsonConvert.SerializeObject(new { code = "0", msg = "查询成功", data = dataList, count = total });

        }

        [HttpPost]
        public async Task< JsonResult> Add(ChapterEntity entity)
        {
            if (entity != null)
            {
                var result =  await chapterService.Add(entity);
                return Json(new { State = "OK", Msg = "添加成功" });
            }
            return Json(new { State = "FAIL", Msg = "添加错误，请稍后重试。" });
        }

        [HttpPost]
        public async Task<JsonResult> Del(int id = 0)
        {
            if (id != 0)
            {
                var result = await chapterService.Del(id);
                return Json(new { state = "ok", msg = "删除成功" });
            }
            return Json(new { state = "fail", msg = "标识错误" });
        }

    }
}
